/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -10 -09
 * Time: 21:37
 */
public class Test3 {
    static long sum1 = 0;
    static long sum2 = 0;
    public static void main(String[] args) {
        long stime = System.currentTimeMillis();
        int[] arr = new int[10000000];
        for (int i = 0; i < 10000000; i++) {
            arr[i] = (int) (Math.random() * 100 + 1);
        }
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < arr.length; i+=2) {
                    sum1 += arr[i];
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 1; i < arr.length; i+=2) {
                    sum2 += arr[i];
                }
            }
        };
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
            long etime = System.currentTimeMillis();
            System.out.printf("执行时长：%d 毫秒.", (etime - stime));
            System.out.println("sum1:" + sum1 + " sum2:" + sum2 + " total:" + (sum1+ sum2));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
